<?php
require_once dirname(__FILE__) . '/../config/config.php';
class DeviceController
{
    private $smarty;
    public function __construct()
    {
    }

    public function main(&$smarty)
    {
        $this->smarty = $smarty;
    }

    public function listAction()
    {
        $context = new BaseContext();
        $result = new ServiceResult();
        $deviceService = new DeviceService();
        
        $deviceArr = $deviceService -> getFullDevice($context, $result);
        
        $this->smarty->assign('deviceArr', $deviceArr);
        $this->smarty->display('list-device.tpl');
    }

    public function addAction()
    {
        $_SESSION['timeout'] = time();
        $context = new BaseContext();
        $result = new ServiceResult();
		$deviceService = new DeviceService();
        
        if(isset($_POST) && !empty($_POST))
        {       
            $param = $_POST;
            $deviceService->insertDevice($context, $result, $param);
            header("location:".WEB_BASE_URL."/index.php?controller=device&action=list");
        }
        else
        {
        	$this->smarty->display('add-device.tpl');
        }
    }

    public function updateAction()
    {
        $_SESSION['timeout'] = time();
    	if(isset($_GET['id']) && !empty($_GET['id']))
		{
		  	$id = $_GET['id'];
		}
        $context = new BaseContext();
        $result = new ServiceResult();
       	$deviceService = new DeviceService();
        
        $device = $deviceService -> getDeviceByID($context, $result, $id);
      
        if(isset($_POST) && !empty($_POST))
        {
            $_POST['id'] = $_POST['id'];
            $param = $_POST;
            $deviceService->updateDevice($context, $result, $param);

            header("location:".WEB_BASE_URL."/index.php?controller=device&action=list");
        }
        $this->smarty->assign('device', $device);
     	$this->smarty->display('update-device.tpl');
    }
    
	public function deleteAction()
    {
        $_SESSION['timeout'] = time();
    	if(isset($_GET['id']) && !empty($_GET['id']))
		{
		  	$id = $_GET['id'];
		}
        $context = new BaseContext();
        $result = new ServiceResult();
       	$deviceService = new DeviceService();
        
        $deviceService->deleteDevice($context, $result, $id);

        header("location:".WEB_BASE_URL."/index.php?controller=device&action=list");
     	//$this->smarty->display('update-device.tpl');
    }

}

?>